package com.va.yahooweather.parser;

import com.va.yahooweather.commons.DateUtil;
import com.va.yahooweather.entity.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;

public class YahooParser {
    
    public YReport read(String log_filename) {
        YReport ret = new YReport();
        
        try {
            Document doc =YahooXMLReader.INSTANCE.read(log_filename);
            
            ret.setLastbuildDate(parseLastbuildDate(doc));
            ret.setLocation(parseLocation(doc));
            ret.setUnits(parseUnits(doc));
            ret.setWind(parseWind(doc));
            ret.setImage(parseImageLink(doc));
            ret.setCondition(parseCondition(doc));
            ret.setForecast(parseForeCast(doc));
            
        } catch (DocumentException ex) {
            java.util.logging.Logger.getLogger(YReport.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return ret;
    }

    private Date parseLastbuildDate(Document doc) {
        return DateUtil.parseDate(doc.valueOf("/rss/channel/lastBuildDate"));
    }
    
    private YLocation parseLocation(Document doc) {
        YLocation location = new YLocation();
        location.setCity( doc.valueOf("/rss/channel/yweather:location/@city") );
        location.setRegion( doc.valueOf("/rss/channel/yweather:location/@region") );
        location.setCountry( doc.valueOf("/rss/channel/yweather:location/@country") );
        return location;
    }

    private YUnit parseUnits(Document doc) {
        YUnit units = new YUnit();
        
        units.setTemperature(doc.valueOf("/rss/channel/yweather:units/@temperature"));
        units.setDistance(doc.valueOf("/rss/channel/yweather:units/@distance"));
        units.setPressure(doc.valueOf("/rss/channel/yweather:units/@pressure"));
        units.setSpeed(doc.valueOf("/rss/channel/yweather:units/@speed"));
        
        return units;
    }
    
    private YWind parseWind(Document doc) {
        YWind wind = new YWind();
        
        wind.setChill(Integer.parseInt(doc.valueOf("/rss/channel/yweather:wind/@chill")));
        wind.setDirection(Integer.parseInt(doc.valueOf("/rss/channel/yweather:wind/@direction")));
        wind.setSpeed(Double.parseDouble(doc.valueOf("/rss/channel/yweather:wind/@speed")));
        
        return wind;
    }
    
    private YImage parseImageLink(Document doc) {
        YImage image = new YImage();
        
        image.setTitle(doc.valueOf("/rss/channel/image/title"));
        image.setUrl(doc.valueOf("/rss/channel/image/url"));
        image.setLink(doc.valueOf("/rss/channel/image/link"));
        image.setWidth(Integer.parseInt(doc.valueOf("/rss/channel/image/width")));
        image.setHeight(Integer.parseInt(doc.valueOf("/rss/channel/image/height")));
        
        return image;
    }
    
    private YCondition parseCondition(Document doc) {
        YCondition condition = new YCondition();
        
        condition.setCode(Integer.parseInt(doc.valueOf("/rss/channel/item/yweather:condition/@code")));
        condition.setText(doc.valueOf("/rss/channel/item/yweather:condition/@text"));
        condition.setTemp(Integer.parseInt(doc.valueOf("/rss/channel/item/yweather:condition/@temp")));
        condition.setDate(DateUtil.parseDate(doc.valueOf("/rss/channel/item/yweather:condition/@date")));
        
        return condition;
    }
    
    private List<YForeCast> parseForeCast(Document doc) {
        List<YForeCast> forecasts = new ArrayList<YForeCast>();
        
        List nodes = doc.selectNodes("/rss/channel/item/yweather:forecast");
        for (Iterator nodeIter = nodes.iterator(); nodeIter.hasNext(); ) {
            Element e = (Element) nodeIter.next();
            
            for ( Iterator iter = e.attributeIterator(); iter.hasNext(); ) {
                YForeCast aCast = new YForeCast();
                Attribute attribute = null;

                attribute = (Attribute) iter.next();
                String day = attribute.getValue();
                aCast.setDay(day);

                attribute = (Attribute) iter.next();
                String date = attribute.getValue();
                aCast.setDate(DateUtil.parseDate(date));

                attribute = (Attribute) iter.next();
                String low = attribute.getValue();
                aCast.setLow(Integer.parseInt(low));

                attribute = (Attribute) iter.next();
                String high = attribute.getValue();
                aCast.setHigh(Integer.parseInt(high));

                attribute = (Attribute) iter.next();
                String text = attribute.getValue();
                aCast.setText(text);

                attribute = (Attribute) iter.next();
                String code = attribute.getValue();
                aCast.setCode(Integer.parseInt(code));
                
                forecasts.add(aCast);
            }
            
            
        }
        
        return forecasts;
    }
}
